package de.hs_rm.mmk.group_talk.activities;

import java.net.InetAddress;
import java.net.UnknownHostException;

import de.hs_rm.mmk.group_talk.network_communication.Communication;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class CreateGroup extends Activity{
	EditText groupname;
	Spinner speechDuration;
	String itemText;
	String speechMod;
	String nickname;
	String serverhost; 
	int serverport;
	int myport;
	Communication comm;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.create_group);
        
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        
        
        //Get SharedPreferences
        final SharedPreferences preferences = Preferences.getPreferences(this);
        nickname = preferences.getString("nickname", ""); 
        serverhost = preferences.getString("serverhost", "");
        serverport = Integer.valueOf(preferences.getString("serverport", "0"));
        //myport = Integer.valueOf(preferences.getString("myport", "0"));
        try {
        	InetAddress iadress = InetAddress.getByName(serverhost);
	        serverhost = iadress.getHostAddress();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
        // Get Views from XML
        groupname = (EditText)findViewById(R.id.createGroup_name);
        speechDuration = (Spinner)findViewById(R.id.createGroup_speakDuration);
        
        // Initialize Spinner for Number of Players
        Spinner spUsers = (Spinner) findViewById(R.id.createGroup_userQuantity);    
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(           
        		this, R.array.sp_groupConfigUserQuantity_array, android.R.layout.simple_spinner_item);    
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);    
        spUsers.setAdapter(adapter);
        
        // Initialize Spinner for SpeakToken
        Spinner spToken = (Spinner) findViewById(R.id.createGroup_token);    
        adapter = ArrayAdapter.createFromResource(           
        		this, R.array.sp_groupConfigToken_array, android.R.layout.simple_spinner_item);    
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);    
        spToken.setAdapter(adapter);
        spToken.setOnItemSelectedListener(new OnItemSelectedListener() {
			
        	@Override
			public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
				Object item = parent.getItemAtPosition(pos);
				speechMod = item.toString();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {}
		});
        
        // Initialize Spinner for SpeakDuration 
        Spinner spDuration = (Spinner) findViewById(R.id.createGroup_speakDuration);    
        adapter = ArrayAdapter.createFromResource(           
        		this, R.array.sp_groupConfigDuration_array, android.R.layout.simple_spinner_item);    
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);    
        spDuration.setAdapter(adapter);
        spDuration.setOnItemSelectedListener(new OnItemSelectedListener() {
			
        	@Override
			public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
				Object item = parent.getItemAtPosition(pos);
				itemText = item.toString();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {}
		});
    }
	
	public void openTalkmasterActivity(final View view) {
		
		//comm = Communication.getInstance(serverhost, serverport);		
		//comm.createNewGroup(groupname.getText().toString());
		
		Intent intent = new Intent(getApplicationContext(),  TalkmasterView.class);
		intent.putExtra("Groupname", String.valueOf(groupname.getText()));
		intent.putExtra("SpeechDuration", itemText);
		intent.putExtra("SpeechMod", speechMod);
		startActivity(intent);
	}
}
